package core{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import alternativa.engine3d.controllers.WalkController;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.display.View;
	import alternativa.types.Point3D;
	import alternativa.engine3d.core.Object3D;
	public class OrbitCamera3D extends Camera3D {
		public var viewCamera:Camera3D;
		public var controller:WalkController;
		public var onStartMoving:Function;
		public var onStopMoving:Function;
		public var onStartDragging:Function;
		public var onStopDragging:Function;
		private var eventsSourceObject:DisplayObject;
		public function OrbitCamera3D( eventsSourceObject:DisplayObject, view:View, distance:Number = 500 ) {
			super();
			viewCamera = new Camera3D();
			addChild( viewCamera );
			this.distance = distance;
			view.camera = viewCamera;
			this.eventsSourceObject = eventsSourceObject;
		};
		public function set distance( value:Number ):void {
			viewCamera.z = -value;
		};
		public function get distance():Number {
			return -viewCamera.z;
		};
		public function lookAt( point:Point3D = null ):void {
			controller.coords = point;
		};
		public function processInput():void {
			if( controller ) {
				controller.processInput();
			} else if( Camera3D(this).parent ) {
				buildController();
			};
		};
		public function set focalPointX( value:Number ):void { controller.coords = new Point3D(value, focalPointY, focalPointZ); };
		public function set focalPointY( value:Number ):void { controller.coords = new Point3D(focalPointX, value, focalPointZ); };
		public function set focalPointZ( value:Number ):void { controller.coords = new Point3D(focalPointX, focalPointY, value); };
		public function get focalPointX():Number { return controller.coords.x; };
		public function get focalPointY():Number { return controller.coords.y; };
		public function get focalPointZ():Number { return controller.coords.z; };
		private function buildController():void {
			controller = new WalkController( eventsSourceObject );
			controller.object = this;
			controller.unbindAll()
			controller.lookAt( new Point3D( 0, 0, 0 ) );
			controller.onStartMoving = function() {
				if( onStartMoving != null ) onStartMoving(); 
			};
			controller.onStopMoving = function() {
				if( onStopMoving != null ) onStopMoving(); 
			};
			eventsSourceObject.addEventListener( MouseEvent.MOUSE_DOWN, _onStartDragging );
			eventsSourceObject.addEventListener( MouseEvent.MOUSE_UP, _onStopDragging );
			addEventListener( MouseEvent.MOUSE_UP, _onStopDragging );
		};
		private function _onStartDragging( event:MouseEvent ):void {
			if( onStartDragging != null ) onStartDragging(); 
		};
		private function _onStopDragging( event:MouseEvent ):void {
			if( onStopDragging != null ) onStopDragging(); 
		};
	};
};